package org.mapdroid.maps.gmaps.overlay.info;

import java.util.Hashtable;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.mapdroid.R;
import org.mapdroid.maps.gmaps.MdMapActivity;
import org.mapdroid.maps.gmaps.overlay.MdMarkerItemizedOverlay;
import org.mapdroid.search.SearchAddressView;
import org.mapdroid.utils.MdDirectionsActivity;

import android.app.Activity;
import android.content.Intent;
import android.location.Address;
import android.location.Location;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class AddressInfoActivity extends Activity implements IMarkerInfo{
	private ListView listView;
	private SimpleAdapter adapter;
	private Address address;
	//private ArrayList<MdService> serviceList;
	
	public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.marker_info_layout);
        
        listView 	= (ListView) findViewById(R.id.info_list);
        listView.setOnItemClickListener(this);
        
        Intent markerIntent 	= super.getIntent();
        String addressString = markerIntent.getStringExtra(MdMarkerItemizedOverlay.STRING_DATA);
        address = markerIntent.getParcelableExtra(MdMarkerItemizedOverlay.OBJECT);
        //serviceList = markerIntent.getParcelableArrayListExtra(MdMarkerItemizedOverlay.LIST);
        
        initDialog(addressString, R.layout.marker_single_voice_layout);
    }
    
    @Override
    public void onDestroy() {
    	super.onDestroy();
    }
    
    @Override
    public void onPause(){
    	super.onPause();
    }

    @Override
    public void onResume(){
    	super.onResume();
    }
    
    @Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.address_info_menu, menu);
	    return true;
	}
    
	@Override
	public void initDialog(Object p, int layoutResId){
		String[] tmpStringVector = new String[3];
		tmpStringVector[0]="Label";
		tmpStringVector[1]="Value";
		tmpStringVector[2]="Image";
		
		int[] tmpId = new int[3];
		tmpId[0] = org.mapdroid.R.id.marker_voice_label;
		tmpId[1] = org.mapdroid.R.id.marker_voice_text;
		tmpId[2] = org.mapdroid.R.id.marker_icon;
		
		Map<String, Object> map = new Hashtable<String, Object>();
		List<Map<String, Object>> addressList = new LinkedList<Map<String, Object>>();
		
		map.put("Label", getString(R.string.address_label));
		map.put("Value", (String) p);
		map.put("Image", android.R.drawable.ic_menu_directions);
		
		addressList.add(map);
		
		adapter = new SimpleAdapter(this, addressList, layoutResId, tmpStringVector, tmpId);
		listView.setAdapter(adapter);
	}
    
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
	}
	
	@Override
    public boolean onOptionsItemSelected(MenuItem item) {
		String itemTitle = (String) item.getTitle();
		
		//Search near services
		if(itemTitle.contentEquals(getString(R.string.search_service_around_item))){
			Location loc = new Location(LocationManager.GPS_PROVIDER);
			loc.setLatitude(address.getLatitude());
			loc.setLongitude(address.getLongitude());
			Intent intent = new Intent();
			intent.putExtra(MdMarkerItemizedOverlay.RANGE, true);
			intent.putExtra(MdMarkerItemizedOverlay.POSITION, loc);
			setResult(Activity.RESULT_OK, intent);
			finish();
		}
			
		//StreetView
		if(itemTitle.contentEquals(getString(R.string.service_on_street_view_item))){
			Intent streetViewIntent = new Intent(Intent.ACTION_VIEW);
			
			streetViewIntent.setData(Uri.parse("google.streetview:cbll=" + 
												address.getLatitude() + 
												"," + 
												address.getLongitude() + 
												"&cbp=1,0,,0,1.0&mz=1"));
			this.startActivity(streetViewIntent);
		}
		
		if(itemTitle.contentEquals(getString(R.string.show_indication_item))){
			Location loc = new Location(LocationManager.GPS_PROVIDER);
			Intent directionsIntent = new Intent(this, MdDirectionsActivity.class);
			loc.setLatitude(address.getLatitude());
			loc.setLongitude(address.getLongitude());
			directionsIntent.putExtra(MdDirectionsActivity.START_POINT, loc);
			directionsIntent.putExtra(MdDirectionsActivity.START_TEXT, SearchAddressView.getAddressString(address));
			startActivityForResult(directionsIntent, MdMapActivity.SEARCH_ADDRESS_INTENT_ID);
		}
		return false;
	}
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data){
		super.onActivityResult(requestCode, resultCode, data);
		
		switch(requestCode){
			case MdMapActivity.SEARCH_ADDRESS_INTENT_ID:
				setResult(Activity.RESULT_OK, data);
				finish();
			break;
		}
	}
}